<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Order;
use App\UserAccount;
use App\User;
use App\UserCoupon;
use App\BankShop;
use App\BankShop_equipment;
use Illuminate\Support\Facades\Auth;

class HomeController extends Controller
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        $this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
    	//----------------检索---------------------------
	    $e_codeS = '';
	    $shopS = '';
	    $order_xdArr = array();
	    $order_thArr = array();
	    // 设备编号
	    if($request->input('e_codeS')){
		    $e_codeS = trim($request->input('e_codeS'));
	    }
	    /**
	     *  判断当前登录账号，如果是第三方网点公司账号，则查询显示当前网点的订单
	     *  如果不是，则默认显示所有网点订单
	     */
	    $adminUser = Auth::user();
	    $phone = $adminUser->name;
	    $bank_id = BankShop::getIdByPhone($phone);
	    if($bank_id){
		    // 网点存在，查询对应网点数据
		    $data['style_display'] = 1;   // style:none
		    $shopS = $bank_id;
		    $data['bankShop'] = BankShop::getTheOneBankShopId($bank_id);
		    $data['bankShopEquipment'] = BankShop_equipment::getTheOneBankShopEquipmentEcode($bank_id);
		    $data['bankShopEquipmentTotal'] = BankShop_equipment::where('state','!=','-1')->where('bank_show_id',$bank_id)->count();

		    $data['orderTotal'] = Order::all()->where('bank_id',$bank_id)->count();// 总订单数
		    $data['orderPendingPayment'] = Order::where('state',1)->where('bank_id',$bank_id)->count(); // 待支付
		    $data['orderPendingUse'] = Order::where('state',2)->where('bank_id',$bank_id)->count(); // 待使用
		    $data['orderUseIn'] = Order::where('state',3)->where('bank_id',$bank_id)->count(); // 使用中
		    $data['orderCompleted'] = Order::where('state',4)->where('bank_id',$bank_id)->count(); // 已完成
		    $data['orderRefund'] = Order::where('state',5)->where('bank_id',$bank_id)->count(); // 退款中
		    $data['orderRefunded'] = Order::where('state',6)->where('bank_id',$bank_id)->count(); // 已退款
		    $data['orderClosed'] = Order::where('state',8)->where('bank_id',$bank_id)->count(); // 已取消

		    $data['userCouponTotal'] = UserCoupon::all()->where('bank_show_id',$bank_id)->count(); // 全部网点券领取总数
		    $data['userUseTotal'] = UserCoupon::where('useStatus',1)->where('bank_show_id',$bank_id)->count(); // 未使用
		    $data['userNoUseTotal'] = UserCoupon::where('useStatus',2)->where('bank_show_id',$bank_id)->count(); // 已使用
		    $data['userExpiredTotal'] = UserCoupon::where('useStatus',3)->where('bank_show_id',$bank_id)->count(); // 已过期
	    }else{
		    //查询全部数据
		    $data['style_display'] = '';
		    if($request->input('shopS')){
			    $shopS = trim($request->input('shopS'));
		    }
		    $data['bankShop'] = BankShop::getAllBankShopId(); // 获取全部网点
		    $data['bankShopEquipment'] = BankShop_equipment::getEquipmentEcode(); // 获取全部设备编号及名称数组
		    $data['bankShopEquipmentTotal'] = BankShop_equipment::where('state','!=',-1)->count(); //总设备个数

		    $data['orderTotal'] = Order::all()->count();// 总订单数
		    $data['orderPendingPayment'] = Order::where('state',1)->count(); // 待支付
		    $data['orderPendingUse'] = Order::where('state',2)->count(); // 待使用
		    $data['orderUseIn'] = Order::where('state',3)->count(); // 使用中
		    $data['orderCompleted'] = Order::where('state',4)->count(); // 已完成
		    $data['orderRefund'] = Order::where('state',5)->count(); // 退款中
		    $data['orderRefunded'] = Order::where('state',6)->count(); // 已退款
		    $data['orderClosed'] = Order::where('state',8)->count(); // 已取消

		    $data['userCouponTotal'] = UserCoupon::all()->count(); // 全部网点券领取总数
		    $data['userUseTotal'] = UserCoupon::where('useStatus',1)->count(); // 未使用
		    $data['userNoUseTotal'] = UserCoupon::where('useStatus',2)->count(); // 已使用
		    $data['userExpiredTotal'] = UserCoupon::where('useStatus',3)->count(); // 已过期
	    }

	    $data['e_codeS'] = $e_codeS;
	    $data['shopS'] = $shopS;

	    // 下单量及金额统计
	   $ordersAll= Order::getOrderBankShowEcodeTotal($e_codeS,$shopS);
	   if($ordersAll['order_xd']){
		   $order_xdArr = $ordersAll['order_xd'][0];
	   }
		if($ordersAll['order_th']){
			$order_thArr = $ordersAll['order_th'][0];
		}

	    $data['order_xd'] = $order_xdArr;
	    $data['order_th'] = $order_thArr;
		//----------------end-------------------
//	    $order_xd = Order::where('state','>',1)->where('state','<=',4)->sum('order_money');
//	    $order_th = Order::where('state','=',6)->sum('order_money');
//	    $data['moneyTotal'] = $order_xd-$order_th; //总收入金额

	    //		dd($order_xdArr);
	    if($order_xdArr->xdMoney){
		    $data['moneyTotal'] = $order_xdArr->xdMoney; //总收入金额
	    }else{
		    $data['moneyTotal'] = 0;
	    }



	    $data['userAccountTotal'] = userAccount::all()->count(); //微信用户总数
	    $data['bankShopTotal'] = BankShop::where('state','!=',-1)->count(); //总网点个数

        return view('home')->with($data);
    }
}
